package com.zsrt.java.ex;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import org.junit.Test;

public class FileTest {
	
	@Test
	public void FileTest() throws Exception {
		File file=new File("c:\\A.txt");
		if(file.exists()){
			String name=file.getName();
			String filePath=file.getAbsolutePath();
			String parent=file.getParent();
			boolean bool=file.canWrite();
			System.out.println("name"+name);
			System.out.println("absolutepath"+filePath);
			System.out.println("parent"+parent);
			System.out.println("boolean canwrite"+bool);
			
			
		}else{
			System.out.println("itisnotexists");
			
		}
		try {
			file.createNewFile();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	@Test
	public void FileInputStream() throws Exception {
		
		try {
			File file=new File("c:\\A.txt");
			
			//选择输入流
			FileInputStream fis=new FileInputStream(file);
			int b=0;
			//从文件读出数据 如果已达到文件的末尾 则返回-1
			while((b=fis.read())!=-1){
				//把读到字节打印出来
				System.out.println((char)b);
				
			}
				fis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	
	@Test
	public void FileOutputStream() throws Exception {
		File f=new File("c://A.txt");
		
		try {
			FileOutputStream fos=new FileOutputStream(f);
			String s="欢迎xxoo";
			//往A。txt写内容
			fos.write(s.getBytes());
			fos.close();
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
		
	}
	
	
	@Test
	public void FileReader() throws Exception {
		try {
			File file=new File("c:\\A.txt");
			//选择输入流 以字符的形式
			FileReader fis=new FileReader(file);
			
			int b=0;
			while((b=fis.read())!=-1){
				System.out.println((char)b);
			}
			
			fis.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	@Test
	public void InputStreamReader() throws Exception {
		
		try {
			File f=new File("c:\\A.txt");
			FileInputStream fis=new FileInputStream(f);
			InputStreamReader isr=new InputStreamReader(fis);
			int i=0;
			while((i=isr.read())>=0){
				System.out.println((char)i);
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		
	}
	
	
	@Test
	public void OutputStreamWiter() throws Exception {
		File f=new File("c:\\A.txt");
		FileOutputStream fos=new FileOutputStream(f);
		OutputStreamWriter osw=new OutputStreamWriter(fos);
		String s="adsd好帅";
		osw.write(s);
		osw.close();
		
		
	}
	

}
